package com.pm.service.ledger.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pm.entity.po.file.FileLib;
import com.pm.entity.po.ledger.DataAnalysisPo;
import com.pm.entity.po.ledger.DataAnalysisTaskPo;
import com.pm.entity.po.ledger.Ledger;
import com.pm.entity.po.security.User;
import com.pm.entity.vo.ledger.DataAnalysisVo;
import com.pm.entity.vo.ledger.LedgerVo;
import com.pm.mapper.awardPunishment.AwardPunishmentMapper;
import com.pm.mapper.file.FileLibMapper;
import com.pm.mapper.ledger.LedgerMapper;
import com.pm.mapper.negative.NegativeChecklistMapper;
import com.pm.mapper.task.TaskMapper;
import com.pm.service.ledger.LedgerService;
import com.pm.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class LedgerServiceImpl implements LedgerService {
    @Autowired
    LedgerMapper ledgerMapper;
    @Autowired
    FileLibMapper fileLibMapper;
    @Autowired
    TaskMapper taskMapper;
    @Autowired
    NegativeChecklistMapper negativeChecklistMapper;
    @Autowired
    AwardPunishmentMapper awardPunishmentMapper;

    @Override
    public PageInfo<Ledger> getAllLedger(Ledger ledger) {
        PageHelper.startPage(ledger.getPageNum(), ledger.getPageSize());
        List<Ledger> ledger1 = ledgerMapper.getAllLedger(ledger);
        PageInfo<Ledger> pageInfo = new PageInfo<>(ledger1);
        PageHelper.clearPage();
        return pageInfo;
    }

    @Override
    public Boolean deleteLedger(Integer id) {
        return ledgerMapper.deleteLedger(id);
    }

    @Override
    public Integer addLedger(Ledger ledger, User user) {
        FileLib fileLib = new FileLib();
        ledger.setMonth(DateUtil.getMonth(ledger.getOccurrenceTime()));
        ledger.setYear(DateUtil.getYear(ledger.getOccurrenceTime()));
        Integer ledger1 = ledgerMapper.addLedger(ledger);
        if (ledger.getFiles().size() != 0) {
            fileLib.setUserId(user.getUserId());
            fileLib.setFiles(ledger.getFiles());
            fileLib.setAimsId(ledger.getId());
            fileLib.setType(ledger.getFileLibType());
            fileLib.setFileAddTime(new Date());
            fileLibMapper.add(fileLib);
        }
        return ledger1;
    }

    @Override
    public Boolean updateLedger(Ledger ledger, User user) {
        if (ledger.getFiles().size() != 0) {
            FileLib fileLib = new FileLib();
            ledger.setUpdateTime(new Date());
            fileLib.setUserId(user.getUserId());
            fileLib.setFiles(ledger.getFiles());
            fileLib.setAimsId(ledger.getId());
            fileLib.setType(ledger.getFileLibType());
            fileLib.setFileAddTime(new Date());
            fileLibMapper.add(fileLib);
        }
        return ledgerMapper.updateLedger(ledger);
    }

    @Override
    public Ledger getLedgerId(Integer id) {
        Ledger ledger = new Ledger();
        ledger = ledgerMapper.getLedgerId(id);
        ledger.setDepartmentName(taskMapper.getDepartmentName(ledger.getTaskId()));
        return ledger;
    }

    /**
     * @param dataAnalysisVo
     * @return xzj
     * 日常工作台账数据分析
     */
    @Override
    public DataAnalysisPo DataAnalysis(DataAnalysisVo dataAnalysisVo) {
        DataAnalysisPo dataAnalysisPo = new DataAnalysisPo();
        // 日常
        dataAnalysisPo.setLedger(ledgerMapper.getTaskNum(dataAnalysisVo));
        // 负面
        dataAnalysisPo.setNegative(negativeChecklistMapper.getTaskNum(dataAnalysisVo));
        // 加分
        dataAnalysisPo.setAwardPlus(awardPunishmentMapper.getTaskPlus(dataAnalysisVo));
        // 减分
        dataAnalysisPo.setReduce(awardPunishmentMapper.getTaskReduce(dataAnalysisVo));
        // 基础分
        dataAnalysisPo.setBaseScore(negativeChecklistMapper.getTeamScoreByTaskId(dataAnalysisVo));
        dataAnalysisPo.setTaskId(dataAnalysisVo.getTaskId());
        dataAnalysisPo.setTaskName(taskMapper.getTaskName(dataAnalysisVo.getTaskId()));

        return dataAnalysisPo;
    }

    @Override
    public List<DataAnalysisTaskPo> DataAnalysisTask(Integer id) {
        List<DataAnalysisTaskPo> dataAnalysisTaskPos = new ArrayList<>();
        // 日常
        if (id == 1) {
            dataAnalysisTaskPos = ledgerMapper.getAnalysisTask();
            return dataAnalysisTaskPos;
        }
        // 负面清单
        if (id == 2) {
            dataAnalysisTaskPos = negativeChecklistMapper.getAnalysisTask();
            return dataAnalysisTaskPos;
        }
        // 加分
        if (id == 3) {
            dataAnalysisTaskPos = awardPunishmentMapper.getAnalysisTaskPlus();
            return dataAnalysisTaskPos;
        }
        // 减分
        if (id == 4) {
            dataAnalysisTaskPos = awardPunishmentMapper.getAnalysisTaskReduce();
            return dataAnalysisTaskPos;
        }
        return null;
    }

    @Override
    public List<DataAnalysisTaskPo> getDataAnalysisTask(LedgerVo ledgerVo) {
        return ledgerMapper.getDataAnalysisTask(ledgerVo);
    }
}
